import numpy as np


def accuracy(y_predict, y_test):
    """分类误差"""
    assert y_predict.shape[0] == y_test.shape[0]
    return sum(y_predict == y_test) / len(y_test)


def mse(y_predict, y_test):
    """均方误差"""
    assert y_predict.shape[0] == y_test.shape[0]
    return np.sum((y_predict - y_test) ** 2) / len(y_test)


def rmse(y_predict, y_test):
    """均方跟误差"""
    assert y_predict.shape[0] == y_test.shape[0]
    return np.sqrt(np.sum((y_predict - y_test) ** 2) / len(y_test))


def mae(y_predict, y_test):
    """平均误差"""
    assert y_predict.shape[0] == y_test.shape[0]
    return np.sum(np.absolute(y_predict - y_test)) / len(y_test)


def r2_score(y_predict, y_test):
    """平均误差"""
    assert y_predict.shape[0] == y_test.shape[0]
    return 1 - (mse(y_predict, y_test) / np.var(y_test))


def TN(y_test, y_predict):
    return np.sum((y_test == 0) & (y_predict == 0))


def FN(y_test, y_predict):
    return np.sum((y_test == 0) & (y_predict == 1))


def FP(y_test, y_predict):
    return np.sum((y_test == 1) & (y_predict == 0))


def TP(y_test, y_predict):
    return np.sum((y_test == 1) & (y_predict == 1))


def precision(y_test, y_predict):
    return TP(y_test, y_predict) / (TP(y_test, y_predict) + FN(y_test, y_predict))


def recall(y_test, y_predict):
    return TP(y_test, y_predict) / (TP(y_test, y_predict) + FP(y_test, y_predict))


def f1_score_(y_test, y_predict):
    return 2 * precision(y_test, y_predict) * recall(y_test, y_predict) / (
        precision(y_test, y_predict) + recall(y_test, y_predict))


def confusion_matrix(y_test, y_predict):
    return np.array([
        [TN(y_test, y_predict), FN(y_test, y_predict)],
        [FP(y_test, y_predict), TP(y_test, y_predict)]
    ])

def TPR(y_test,y_predict):
    return recall(y_test,y_predict)

def FPR(y_test,y_predict):
    return FP(y_test, y_predict) / (TN(y_test, y_predict) + FP(y_test, y_predict))